package com.ehome.poct.backend.controller.device;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.ehome.check.utils.ExcelUtils;
import com.ehome.exception.BizException;
import com.ehome.poct.base.api.dto.device.BootInfoExportQry;
import com.ehome.poct.base.api.feign.device.TestDeviceBootingDetailFeginClient;
import com.ehome.poct.base.api.vo.device.BootDetailExportVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 开机自检记录详细控制器
 *
 * @author laobai
 * @Date 2022-10-17 10:11
 */
@Slf4j
@Api(tags = "开机自检记录详细")
@ApiSupport(author = "laobai")
@RestController
@RequiredArgsConstructor
@RequestMapping("/devicebootingdetail")
public class TestDeviceBootingDetailController {
    @Autowired
    TestDeviceBootingDetailFeginClient bootingDetailFeign;
    private final Integer MAX_SHEET_SIZE = 100000;

    /**
     * 获取导出的数据
     *
     * @param qry sno条件
     * @return 组装好的导出数据
     */
    @ApiOperation(value = "导出-设备开机自检记录详细", produces = "application/octet-stream")
    @ApiOperationSupport(author = "laobai")
    @PostMapping("/exportbootinfo")
    public void exportDistributeRecord(@RequestBody BootInfoExportQry qry) {
        List<BootDetailExportVO> exportDistributeDeviceVOS = bootingDetailFeign.getExportDetailData(qry);
        if (CollUtil.isEmpty(exportDistributeDeviceVOS)) {
            log.info("没有可导出的数据");
            return;
        }
        //拿第一个sno 当sheet1
        String firstSheetName = exportDistributeDeviceVOS.stream().map(BootDetailExportVO::getSno).distinct().findFirst().orElse("无数据");
        Map<String, List<BootDetailExportVO>> map = exportDistributeDeviceVOS.stream().collect(Collectors.groupingBy(BootDetailExportVO::getSno, LinkedHashMap::new, Collectors.toList()));
        ExcelWriter writer = new ExcelWriter(true, firstSheetName);
        BootDetailExportVO.EXCEL_HEADER_ALIAS.forEach(writer::addHeaderAlias);
        writer.setOnlyAlias(true);
        writer.write(map.get(firstSheetName), true);
        try {
            for (String key : map.keySet()) {
                if (firstSheetName.equals(key)) continue;
                writer.setSheet(key);
                if (map.get(key).size() <= MAX_SHEET_SIZE) {
                    writer.setOnlyAlias(true);
                    writer.write(map.get(key), true);
                } else {
                    log.error("导出excel失败!不支持数据超过10万！");
                    throw new BizException(201, "导出excel失败!不支持数据超过10万！");
                }
            }
            ExcelUtils.write(writer, "开机自检数据导出");
        } catch (Exception e) {
            log.error("导出失败", e);
            throw new BizException(201, "导出excel失败");
        }
    }
}
